package com.byq.mm.gateway.adapter;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.drawable.Drawable;


public class IconManager {
	static Map<String, Bitmap> icons = new HashMap<String, Bitmap>();
	static Map<String, Drawable> icond = new HashMap<String, Drawable>();
    static public Bitmap getIcon(Context context, String dct) {
    	
    		if(dct.equals("switch"))
    			dct = "switch0";
    		Bitmap ic = icons.get(dct);
    		if(ic == null){
    			int id = context.getResources().getIdentifier(dct.toLowerCase(),  "drawable",context.getPackageName());
    			ic = BitmapFactory.decodeResource(context.getResources(), id);
    			icons.put(dct, ic);
    		}
    		return ic;
    }
    static public String getString(Context context, String dct) {
		String path = context.getPackageName();
    	int id = context.getResources().getIdentifier(dct.toLowerCase(),
				"string", path);
		return context.getResources().getString(id);
    }
	static public Drawable getIconDrawable(Context context, String dct) {
		if (dct.equals("switch"))
			dct = "switch0";
		Drawable ic = icond.get(dct);
		if (ic == null) {
			int id = context.getResources().getIdentifier(dct.toLowerCase(),
					"drawable", context.getPackageName());
			if(id == 0)
				return null;
			ic = context.getResources().getDrawable(id);
			icond.put(dct, ic);
		}
		return ic;
	}
	static Map<String, Drawable> gray = new HashMap<String, Drawable>();

	static public Drawable getGrayIconDrawable(Context context, String dct) {
		if (dct.equals("switch"))
			dct = "switch0";
		Drawable ic = gray.get(dct);
		if (ic == null) {
			String path = context.getPackageName();
			//int id = context.getResources().getIdentifier(dct.toLowerCase() + "_close",
			//		"drawable", path);
			//if(id == 0)
			int	id = context.getResources().getIdentifier(dct.toLowerCase(),
					"drawable", path);
			if(id == 0)
				return null;
			ic = context.getResources().getDrawable(id);
			ic.mutate();
			ColorMatrix cm = new ColorMatrix();
			cm.setSaturation(0);
			ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);
			ic.setColorFilter(cf);
			gray.put(dct, ic);
		}

		
		return ic;
	}
}
